package com.rk.util;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Window;

public class LayoutUtils {

    private static final String TAG = "LayoutUtils";
    private static LayoutUtils mInstance;
    private Activity mActivity;

    public static LayoutUtils getInstance(Activity activity) {
        if(mInstance == null) {
            mInstance = new LayoutUtils(activity);
        }
        return mInstance;
    }

    private LayoutUtils(Activity activity) {
        this.mActivity = activity;
    }

    public List<Integer> getScreenInfo() {
        DisplayMetrics dm = new DisplayMetrics();
        List<Integer> infor = new ArrayList<Integer>();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;
        int screenHeigh = dm.heightPixels;
        infor.add(screenWidth);
        infor.add(screenHeigh);
        return infor;
    }

    @SuppressLint("NewApi") 
    public void getScreenInfo(int n) {
        Display display;
        if (android.os.Build.VERSION.SDK_INT >= 13) {
            display = mActivity.getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            int height = size.y;
        }else {
            display = mActivity.getWindowManager().getDefaultDisplay();
            int width = display.getWidth();
            int height = display.getHeight();
        }
        int width = mActivity.getResources().getDisplayMetrics().heightPixels;
        int height = mActivity.getResources().getDisplayMetrics().widthPixels;
    }

    /* ��ȡ״̬���߶� */
    public int getDecorViewHeigh() {
        Rect frame = new Rect();
        mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        return statusBarHeight;
    }

    public int getTitleBarHeih() {
        int contentTop = mActivity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
        int titleBarHeigh = contentTop - getStatusHeight(mActivity);
        return titleBarHeigh;
    }

    public static int getStatusHeight(Context context) {
        int statusHeight = -1;
        try {
            Class clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    public int getStatusBarHeight() {
        Resources resources = mActivity.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen","android");
        int height = resources.getDimensionPixelSize(resourceId);
        Log.e("dbw", "Status height:" + height);
        return height;
    }

    public int getNavigationBarHeight() {
        Resources resources = mActivity.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        Log.e("dbw", "Navi height:" + height);
        return height;
    }
}
